package com.jic.member.controller.manage.feign;

import com.jic.common.base.vo.RestResult;
import com.jic.member.api.rest.MemberAddressApi;
import com.jic.member.request.UserAddrRequest;
import com.jic.member.request.power.UserConstantsProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/memberAddress")
public class MemberAddressFeign implements MemberAddressApi {

    @Resource
    private StringRedisTemplate iCache;

    private final static String TOKEN_KEY = "stoken";

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserConstantsProperties userConstantsProperties;

    @Override
    public RestResult<?> searchUserAddrList(String token) throws IOException {
        String url = userConstantsProperties.getUocUrlWx() + "/haier/v2/userAddresses";
        //构造请求UOC接口
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new  GetMethod(url);
        getMethod.addRequestHeader("Content-Type", "application/json");
        getMethod.addRequestHeader("Authorization", "Bearer " + token);


        httpClient.executeMethod(getMethod);
        String respstr = getMethod.getResponseBodyAsString();
        return RestResult.success(respstr);
    }

    @Override
    public RestResult<?> searchUserAddr(UserAddrRequest userAddrRequest) throws IOException {
        String url = userConstantsProperties.getUocUrlWx() + "/haier/v2/userAddress";
        //构造请求UOC接口
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new  GetMethod(url + "/" + userAddrRequest.getId());
        getMethod.addRequestHeader("Content-Type", "application/json");
        getMethod.addRequestHeader("Authorization", "Bearer " + userAddrRequest.getToken());


        httpClient.executeMethod(getMethod);
        String respstr = getMethod.getResponseBodyAsString();
        return RestResult.success(respstr);
    }

    /**
     * @param userAddrRequest
     * @description: 添加用户地址
     * @return:
     * @author: fengrui
     * @time: 2020/12/09 22:36
     */
    @Override
    public RestResult<?> addUserAddr(UserAddrRequest userAddrRequest) throws IOException {
        String uuid = UUID.randomUUID().toString();
        log.info(uuid+"====准备调用用户中心保存用户地址====");
        String url = userConstantsProperties.getUocUrlWx() + "/haier/v2/userAddress";
        //构造请求UOC接口
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);

        postMethod.addRequestHeader("Content-Type", "application/json");
        postMethod.addRequestHeader("Authorization", "Bearer " + userAddrRequest.getToken());
        log.info(uuid+"调用用户中心保存用户地址发送请求，参数：" + userAddrRequest.getParam());
        RequestEntity se = new StringRequestEntity(userAddrRequest.getParam(), "application/json", "UTF-8");
        postMethod.setRequestEntity(se);
        log.info(uuid+"发送请求");
        httpClient.executeMethod(postMethod);
        String respstr = postMethod.getResponseBodyAsString();
        log.info(uuid+"调用用户中心保存用户地址返回结果："+ respstr);
        return RestResult.success(respstr);
    }

    /**
     * @description: 修改用户地址
     * @return:
     * @author: fengrui
     * @time: 2020/12/09 22:38
     */
    @Override
    public RestResult<?> updateUserAddr(UserAddrRequest request) throws IOException {
        log.info("====准备调用用户中心修改用户地址====");
        String url = userConstantsProperties.getUocUrlWx() + "/haier/v2/userAddress";
        //构造请求UOC接口
        HttpClient httpClient = new HttpClient();
        PutMethod putMethod = new PutMethod(url + "/" + request.getId());

        putMethod.addRequestHeader("Content-Type", "application/json");
        putMethod.addRequestHeader("Authorization", "Bearer " + request.getToken());
        log.info("调用用户中心修改用户地址发送请求，参数：" + request.getParam());
        RequestEntity se = new StringRequestEntity(request.getParam(), "application/json", "UTF-8");
        putMethod.setRequestEntity(se);
        httpClient.executeMethod(putMethod);
        String respstr = putMethod.getResponseBodyAsString();
        log.info("调用用户中心修改用户地址返回结果："+ respstr);
        return RestResult.success(respstr);
    }

    @Override
    public RestResult<?> deleteUserAddr(UserAddrRequest userAddrRequest) throws IOException {
        //构造请求UOC接口
        HttpClient httpClient = new HttpClient();
        DeleteMethod deleteMethod = new DeleteMethod(userConstantsProperties.getUocUrlWx() + "/" + userAddrRequest.getId());


        deleteMethod.addRequestHeader("Content-Type", "application/json");
        deleteMethod.addRequestHeader("Authorization", "Bearer " + userAddrRequest.getToken());


        httpClient.executeMethod(deleteMethod);
        String respstr = deleteMethod.getResponseBodyAsString();
        return RestResult.success(respstr);
    }

    @Override
    public RestResult<?> getAdminAddress(UserAddrRequest userAddrRequest) throws IOException {
        String url = userConstantsProperties.getUocUrlWx() + "/haier/v2/userAddress/getAdminAddress";
        //构造请求UOC接口
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url + "?superior=" + userAddrRequest.getId());


        postMethod.addRequestHeader("Content-Type", "application/json");
        postMethod.addRequestHeader("Authorization", "Bearer " + userAddrRequest.getToken());


        RequestEntity se = new StringRequestEntity("", "application/json", "UTF-8");
        postMethod.setRequestEntity(se);
        httpClient.executeMethod(postMethod);
        String respstr = postMethod.getResponseBodyAsString();
        return RestResult.success(respstr);
    }

}
